package com.xc.controller.protol;

import com.google.common.collect.Maps;
import com.xc.common.Const;
import com.xc.common.ServerResponse;
import com.xc.service.IFileUploadService;
import com.xc.service.IUserPositionService;
import com.xc.service.IUserService;
import com.xc.utils.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 *  用户中心接口
 * @author mipha
 * @version 1.0
 * @date 2019/2/18 11:06 AM
 */
@Controller
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Autowired
    IUserService iUserService;
    @Autowired
    IUserPositionService iUserPositionService;
    @Autowired
    IFileUploadService iFileUploadService;

    /**
     *      添加自选股
     * @param code
     * @param request
     * @return
     */
    @RequestMapping("addOption.do")
    @ResponseBody
    public ServerResponse addOption(@RequestParam("code") String code
            , HttpServletRequest request){
        return iUserService.addOption(code,request);
    }

    /**
     *      删除自选股
     * @param code
     * @param request
     * @return
     */
    @RequestMapping("delOption.do")
    @ResponseBody
    public ServerResponse delOption(@RequestParam("code") String code
            , HttpServletRequest request){
        return iUserService.delOption(code,request);
    }

    /**
     *  查询股票是否添加自选
     * @param code
     * @param request
     * @return
     */
    @RequestMapping("isOption.do")
    @ResponseBody
    public ServerResponse isOption(@RequestParam("code") String code
            , HttpServletRequest request){
        return iUserService.isOption(code,request);
    }



    /**
     *  用户下单
     * @param stockId   股票id
     * @param buyNum    数量：单位 股
     * @param buyType   类型：涨 / 跌
     * @param lever     杠杆倍数
     * @param request
     * @return
     */
    @RequestMapping("buy.do")
    @ResponseBody
    public ServerResponse buy(@RequestParam("stockId") Integer stockId,
                              @RequestParam("buyNum") Integer buyNum,
                              @RequestParam("buyType") Integer buyType,
                              @RequestParam("lever") Integer lever,
                              HttpServletRequest request){
        ServerResponse serverResponse = null;
        try {
            serverResponse = iUserPositionService
                    .buy(stockId,buyNum,buyType,lever,request);
        }catch (Exception e){
            log.error("用户下单操作 = {}",e);
        }
        return serverResponse;
    }

    /**
     *  用户平仓
     * @param request
     * @param positionSn
     * @return
     */
    @RequestMapping("sell.do")
    @ResponseBody
    public ServerResponse sell(HttpServletRequest request,
                               @RequestParam("positionSn") String positionSn){
        ServerResponse serverResponse = null;
        try {
            serverResponse = iUserPositionService
                    .sell(positionSn, Const.SellStockDoType.normal_do_type);
        }catch (Exception e){
            log.error("用户平仓操作 = {}",e);
        }
        return serverResponse;
    }


    /**
     *      获取用户信息
     * @param request
     * @return
     */
    @RequestMapping("getUserInfo.do")
    @ResponseBody
    public ServerResponse getUserInfo(HttpServletRequest request){
        return iUserService.getUserInfo(request);
    }


    /**
     *  登陆状态下修改密码
     * @param oldPwd    旧密码
     * @param newPwd    新密码
     * @param request
     * @return
     */
    @RequestMapping("updatePwd.do")
    @ResponseBody
    public ServerResponse updatePwd(String oldPwd,String newPwd,HttpServletRequest request){
        return iUserService.updatePwd(oldPwd,newPwd,request);
    }


    /**
     *  用户实名认证
     * @param realName
     * @param idCard
     * @param request
     * @return
     */
    @RequestMapping("auth.do")
    @ResponseBody
    public ServerResponse auth(String realName,String idCard
                            ,String img1key,String img2key,String img3key
                            ,HttpServletRequest request){
        return iUserService.auth(realName ,idCard ,img1key,img2key,img3key,request);
    }


    /**
     *  用户中心文件上传
     * @param session
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("upload.do")
    @ResponseBody
    public ServerResponse upload(HttpSession session
            , @RequestParam(value = "upload_file" , required = false) MultipartFile file
            , HttpServletRequest request){

        String path = request.getSession().getServletContext().getRealPath("upload");

        ServerResponse serverResponse = iFileUploadService.upload(file,path);
        if(serverResponse.isSuccess()){
            String targetFileName = serverResponse.getData().toString();
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")
                    + targetFileName;

            Map fileMap = Maps.newHashMap();
            fileMap.put("uri" , targetFileName);
            fileMap.put("url" , url);

            return ServerResponse.createBySuccess(fileMap);
        }else{
            return serverResponse;
        }
    }







}
